<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
  // Promise 解决回调地狱

  function queryData1 () {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve('tom')
      }, 1000)
    })
  }
  function queryData2 () {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve('jerry')
      }, 2000)
    })
  }
  function queryData3 () {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve('spike')
      }, 3000)
    })
  }

  // queryData1().then(ret => console.log(ret))
  // queryData2().then(ret => console.log(ret))
  // queryData3().then(ret => console.log(ret))

  // 需求：spike -> tom -> jerry

  // 基于Promise解决回调地狱问题，代码嵌套减少了，可读性更高
  // Promise虽然对回调的方式有所改进，但是依然有回调函数的使用
  // 为了进一步改善Promise的问题，就诞生了Async函数（async/await）
  queryData3()
    .then(ret => {
      console.log(ret)
      return queryData1()
    })
    .then(ret => {
      console.log(ret)
      return queryData2()
    })
    .then(ret => {
      console.log(ret)
    })



  </script>
</body>
</html>